/**
 * 
 */
package com.gmail.blackdog1987.carcare.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.gmail.blackdog1987.carcare.App;
import com.gmail.blackdog1987.carcare.BaseActivity;
import com.gmail.blackdog1987.carcare.Constant;
import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.db.dao.UserDao;
import com.gmail.blackdog1987.carcare.entity.TProduct;
import com.gmail.blackdog1987.carcare.network.request.builder.OrderRoadHelpRequestBuilder;
import com.gmail.blackdog1987.carcare.network.response.OrderCarCleanResponse;
import com.gmail.blackdog1987.carcare.network.response.OrderRoadHelpResponse;
import com.gmail.blackdog1987.carcare.network.response.handler.IResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.OnResponseListener;
import com.gmail.blackdog1987.carcare.network.response.handler.OrderRoadHelpResponseHandler;

public class OrderRoadHelpActivity extends BaseActivity implements OnClickListener, OnResponseListener {
	private Context mContext;
	private EditText mName;
	private EditText mPhone;
	private EditText mInfo;
	private TextView mTitle;
	private ImageView mType;
	private TextView mTypeStr;
	private TextView mLocationStr;
	private View mPay;
	private TextView mPrice;
	private View mCall;
	private View mLocation;
	private int type;
	
	private OrderRoadHelpRequestBuilder mReq = new OrderRoadHelpRequestBuilder();
	private OrderRoadHelpResponseHandler mHandler = new OrderRoadHelpResponseHandler();
	Integer price;

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_order_roadhelp);
		mContext = this;
		
		price = App.PRICE_CONFIG.get(Constant.Config.FUNCTION_ROADHELP);
		if(price==null){
			Toast.makeText(mContext, "尚未获得价格，请退出应用，打开网络，重试", Toast.LENGTH_LONG).show();
			finish();
			return;
		}

		initViews();
		initData(savedInstanceState);
		reg();
	}

	public void initViews() {
		mName = (EditText) findViewById(R.id.order_roadhelp_name);
		mCall = findViewById(R.id.order_roadhelp_call);
		mPrice = (TextView) findViewById(R.id.order_roadhelp_cost);
		mLocationStr = (TextView) findViewById(R.id.order_roadhelp_location_str);
		mInfo = (EditText) findViewById(R.id.order_roadhelp_info);
		mLocation = findViewById(R.id.order_roadhelp_location);
		mPay = findViewById(R.id.order_roadhelp_pay);
		mPhone = (EditText) findViewById(R.id.order_roadhelp_phone);
		mTitle = (TextView) findViewById(R.id.order_roadhelp_title);
		mType = (ImageView) findViewById(R.id.order_roadhelp_type);
		mTypeStr = (TextView) findViewById(R.id.order_roadhelp_typestr);
	}

	public void reg() {
		mPay.setOnClickListener(this);
		mHandler.setOnResponseListener(this);
		findViewById(R.id.back).setOnClickListener(this);
	}

	public void initData(Bundle savedInstanceState) {
		type = getIntent().getIntExtra("type", -1);
		if(type==-1){
			Toast.makeText(mContext, "unkown error", Toast.LENGTH_LONG).show();
			finish();
		}else{
			if(type==1){
				mType.setImageResource(R.drawable.bg_roudhelp_luntai);
				mTitle.setText("轮胎救援");
			}else if(type==2){
				mType.setImageResource(R.drawable.bg_roudhelp_dianping);
				mTitle.setText("电瓶救援");
			}else if(type==3){
				mType.setImageResource(R.drawable.bg_roudhelp_qiyou);
				mTitle.setText("汽油救援");
			}else if(type==4){
				mType.setImageResource(R.drawable.bg_roudhelp_daijia);
				mTitle.setText("代驾陪驾");
			}
		}
		mPrice.setText(String.valueOf(price));
	}
	
	private boolean validInput(){
		return true;
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.order_roadhelp_pay:
			if(App.validUser(this)&&validInput()){
				mReq = new OrderRoadHelpRequestBuilder();
				mReq.setInfo(mInfo.getText().toString())
					.setUserid("1")
					.setLat("0")
					.setLon("0")
					.setLocation("")
					.setName(mName.getText().toString())
					.setPhone(mPhone.getText().toString())
					.setType(String.valueOf(type))
					.postJsonRequest(mContext, mHandler);
			}
			break;
		case R.id.back:
			finish();
			break;
		default:
			break;
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode==Activity.RESULT_OK){
			switch(requestCode){
			case PayActivity.REQUESTCODE_PAY:
				Toast.makeText(mContext, "恭喜您支付成功", Toast.LENGTH_LONG).show();
				finish();
				break;
			}
		}
	}

	@Override
	public void onResponse(IResponseHandler classz) {
//		Toast.makeText(mContext, "", Toast.LENGTH_LONG).show();
//		finish();
		TProduct product = new TProduct();
		product.body = "道路救援";
		product.price = Double.valueOf(mPrice.getText().toString());
		product.subject = "车拉丁";
		product.tableName = "t_order_roadhelp";
		product.tableId = ((OrderRoadHelpResponse)classz.getResponse()).order.getId();
		product.userid = new UserDao().getUser().id;
		Intent i = new Intent(OrderRoadHelpActivity.this, PayActivity.class);
		i.putExtra("data", product);
		startActivityForResult(i, PayActivity.REQUESTCODE_PAY);
	}

	@Override
	public void onError(IResponseHandler classz) {
		Toast.makeText(mContext, "下单失败！", Toast.LENGTH_LONG).show();
	}
}
